#!/bin/bash

# 临时加的，避免报错，下个版本删除
ln -s /usr/local/Ascend/ascend-toolkit/8.0.0.alpha003/aarch64-linux/ascendc/include/highlevel_api/kernel_tiling/ /usr/local/Ascend/ascend-toolkit/latest/include/

#rm -rf api
rm -rf output
mkdir output
mkdir output/log
mkdir output/result

start_time=$(date +%s)
echo "start_time:" $start_time


#if [ $? -eq 0 ];then
#    echo "[INFO]:example download success!"
#else
#    echo "[ERROR]:example download fail!"
#fi

cur_path=`pwd`

declare -A example_list
#example_list=([simplesoftmax]="ascendc-api-adv/examples/activation/simplesoftmax/kernel_launch_method_by_framework" [softmax]="ascendc-api-adv/examples/activation/softmax/kernel_launch_method_by_framework" [softmaxflash]="ascendc-api-adv/examples/activation/softmaxflash/kernel_launch_method_by_framework" [softmaxgrad]="ascendc-api-adv/examples/activation/softmaxgrad/kernel_launch_method_by_framework" [softmaxgradfront]="ascendc-api-adv/examples/activation/softmaxgradfront/kernel_launch_method_by_framework" [matmul]="ascendc-api-adv/examples/matrix/matmul/kernel_launch_method_by_framework" [broadcast]="ascendc-api-adv/examples/pad/broadcast/kernel_launch_method_by_framework")
example_list=([simplesoftmax]="ascendc-api-adv/examples/activation/simplesoftmax/kernel_launch_method_by_framework" [softmax]="ascendc-api-adv/examples/activation/softmax/kernel_launch_method_by_framework" [matmul]="ascendc-api-adv/examples/matrix/matmul/kernel_launch_method_by_framework" [layernorm]="ascendc-api-adv/examples/normalization/layernorm/kernel_launch_method_by_framework" [batch_matmul]="ascendc-api-adv/examples/matrix/batch_matmul/kernel_launch_method_by_framework")

for exam in "${!example_list[@]}"
do
    echo "[INFO]:API_NAME is $exam"
    cd ${cur_path}/${example_list[$exam]}
    python3 scripts/gen_data.py

    sed -i 's#"value": "~/Ascend/ascend-toolkit/latest"#"value": "/usr/local/Ascend/ascend-toolkit/latest"#' CMakePresets.json
    export ASCEND_HOME_DIR=/usr/local/Ascend/ascend-toolkit/latest
    source $ASCEND_HOME_DIR/../set_env.sh
    
    chmod +x build.sh
    ./build.sh
    if find build_out -maxdepth 1 -name custom_opp_*.run | read -r;then
	    echo "[INFO]: custom_opp packet build success!"
    else
	    echo "[ERROR]:custom_opp packet build failed!"
    fi

    cd build_out
    ./custom_opp_*.run
    ./${exam}_custom_npu >> ${cur_path}/output/log/train_${exam}.log 2>&1 &
    wait
    echo "--------------------Check run result and log--------------------"
    grep -q "test pass!" ${cur_path}/output/log/train_${exam}.log
    if [ $? -eq 0 ]; then
	    echo "[INFO]:${exam} example result check pass!"
	    result_tmp="pass"
    else
	    echo "[ERROR]:${exam} example result check fail!"
	    result_tmp="fail"
    fi

    grep -q "ERROR" ${cur_path}/output/log/train_${exam}.log
    if [ $? -eq 1 ];then
	    echo "[INFO]:${exam} example log check pass!"
	    log_tmp="pass"
    else
	    echo "[INFO]:${exam} example log check fail!"
	    log_tmp="fail"
    fi

    if [ "${result_tmp}" = "pass" ] && [ "${log_tmp}" = "pass" ];then
	    echo "${exam}_example_testcase_result: pass" | tee -a ${cur_path}/output/result/result.txt
    else
	    echo "${exam}_example_testcase_result: fail" | tee -a ${cur_path}/output/result/result.txt
    fi
    cp ${cur_path}/output/result/result.txt ${cur_path}
done
end_time=$(date +%s)
echo "end_time:" $end_time
e2e_time=$[ $end_time - $start_time ]
echo "Duration:" $e2e_time
